import * as THREE from 'three';
import obruntime from './test/js/obruntime.mjs';

const renderer = new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: true });
renderer.setClearColor(0xffffff, 0);

let updateDefaultScene;
function buildDefaultScene() {
    const scene = new THREE.Scene();
    scene.background = new THREE.Color( 0xffffff );
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);
    const geometry = new THREE.BoxGeometry(1, 1, 1);
    const edges = new THREE.EdgesGeometry(geometry);
    const line = new THREE.LineSegments(edges);
    line.material.depthTest = true;
    line.material.opacity = 0.7;
    line.material.transparent = true;
    line.material.color = 0x0055ff;

    scene.add(line);


    const material = new THREE.MeshStandardMaterial({ color: 0x0055ff, transparent: true, opacity: 0.7 });
    const cube = new THREE.Mesh(geometry, material);
    let scale = 0.5774;
    cube.scale.set(scale, scale, scale);
    scene.add(cube);

    cube.rotation.x += 90;
    cube.rotation.y += 90;

    // const material = new THREE.LineBasicMaterial({ color: 0x0055ff });
    // const cube = new THREE.Mesh(geometry, material);
    // scene.add(cube);

    const light1 = new THREE.PointLight(0xffffff, 1, 0);
    light1.position.set(0, 200, 0);
    scene.add(light1);

    const light2 = new THREE.PointLight(0xffffff, 1, 0);
    light2.position.set(100, 200, 100);
    scene.add(light2);

    const light3 = new THREE.PointLight(0xffffff, 1, 0);
    light3.position.set(- 100, - 200, - 100);
    scene.add(light3);


    // const points = [];
    // points.push( new THREE.Vector3( - 10, 0, 0 ) );
    // points.push( new THREE.Vector3( 0, 10, 0 ) );
    // points.push( new THREE.Vector3( 10, 0, 0 ) );
    // points.push( new THREE.Vector3( 10, 0, 10 ) );
    // points.push( new THREE.Vector3( 0, 10, -10 ) );
    // points.push( new THREE.Vector3( 10, 10, -10 ) );
    // points.push( new THREE.Vector3( 0, 0, 0 ) );

    // const geometry1 = new THREE.BufferGeometry().setFromPoints( points );
    // const line = new THREE.Line( geometry1, material );
    // scene.add( line );

    camera.position.z = 5;

    updateDefaultScene = () => {
        line.rotation.x += 0.01;
        line.rotation.y += 0.01;
        cube.rotation.x += 0.01;
        cube.rotation.y += 0.01;
        renderer.render(scene, camera);
    }
}
buildDefaultScene();
let updateScene = updateDefaultScene;
function animate() {
    requestAnimationFrame(animate);
    updateScene();
}
animate();


obruntime(renderer, (update) => {
    updateScene = () => {
        if (!update()) {
            updateDefaultScene();
        }
    };
});